package com.usian.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.usian.common.R;
import com.usian.pojo.User;
import com.usian.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 登录
     * @param user
     * @param session
     * @return
     */
    @PostMapping("/login")
    public R login(@RequestBody User user, HttpSession session){
        R<User> login = userService.login(user);
        //判断登陆是否成功
        if (login.getCode()==1){
            session.setAttribute("login",login.getData());
        }
        return login;
    }

    /**
     * 退出
     * @param session
     * @return
     */
    @PostMapping("/logout")
    public R logout(HttpSession session){
        session.invalidate();               //让session失效
        //session.removeAttribute("login"); //清空session
        return R.success("退出登录");
    }

}











